#ifndef COMMON_TOKEN_H_
#define COMMON_TOKEN_H_

#include <istream>
#include <cstdint>
#include <string>

namespace fermat {


class  Token {
public:
	enum TType
	{
		IDENTIFIER_TOKEN,
		KEYWORD_TOKEN,
		SEPARATOR_TOKEN,
		OPERATOR_TOKEN,
		STRING_LITERAL_TOKEN,
		CHAR_LITERAL_TOKEN,
		INTEGER_LITERAL_TOKEN,
		LONG_INTEGER_LITERAL_TOKEN,
		FLOAT_LITERAL_TOKEN,
		DOUBLE_LITERAL_TOKEN,
		COMMENT_TOKEN,
		SPECIAL_COMMENT_TOKEN,
		PREPROCESSOR_TOKEN,
		WHITESPACE_TOKEN,
		EOF_TOKEN,
		INVALID_TOKEN,
		USER_TOKEN
	};
	
	/*!
	*Creates the Token.
	*/
	Token(bool ignore = false);

	/*!
	*Destroys the Token.
	*/
	virtual ~Token();
		
	/*!
	* Checks if the given character (and, optionally,
	* the next character in the input stream) start
	* a valid token. Returns true if so, false
	* otherwise.
	*
	* The current read position in istr must not be
	* changed. In other words, only the peek() method
	* of istream may be used.
	*
	* If the character starts the token, it should
	* be set as the token's value.
	*/
	virtual bool start(char c, std::istream& istr);


	/*!
	* Builds the token by reading and appending 
	* the remaining characters from istr.
	*/
	virtual bool finish(std::istream& istr);

	/*!
	* @return the kind of the token.
	*/
	virtual TType tokenClass() const;
		
	/*!
	* @return the token's raw string.
	*/
	const std::string& tokenString() const;
	
	/*!
	* @return a string representation of the token.
	*/
	virtual std::string asString() const;

	/*!
	* @returna 64-bit integer representation of the token.
	*/
	virtual int64_t asInteger64() const;
      
	/*!
	* @returna an unsigned 64-bit integer representation of the token.
	*/
	virtual uint64_t asUnsignedInteger64() const;
 
	/*!
	* @returna an integer representation of the token.
	*/
	virtual int asInteger() const;

	/*!
	* @returna an unsigned integer representation of the token.
	*/
	virtual unsigned asUnsignedInteger() const;

	/*!
	* @returna a floating-point representation of the token.
	*/
	virtual double asFloat() const;

	/*!
	* @returna a char representation of the token.
	*/
	virtual char asChar() const;

	/*!
	* @returna true iff the token has the given class.
	*/
	bool is(TType tokenClass) const;
	
	/*!
	* If ignored is true, the token will be marked
	* as ignorable, which means that next() will
	* not return it.
	* If ignored is false, the token will be marked
	* as acceptable, which means that next() will
	* return it.
	*/
	void ignore(bool ignored);

	/*!
	* @returna if the token is ignored or not
	*/
	bool ignored() const;

protected:
	std::string _value;
	bool		_ignored;
	
private:
	Token(const Token&);
	Token& operator = (const Token&);
};

/*!
* This token class is used for signaling that
* an invalid character sequence has been encountered
* in the input stream.
*/
class  InvalidToken: public Token {
public:
	InvalidToken();
	~InvalidToken();
	TType tokenClass() const;
};


/*!
* This token class is used to signal the
* end of the input stream.
*/
class  EOFToken: public Token {
public:
	EOFToken();
	~EOFToken();
	TType tokenClass() const;
};



/*!
* This pseudo token class is used to eat
* up whitespace in between real tokens.
*/
class  WhitespaceToken: public Token {
public:
	WhitespaceToken();
	~WhitespaceToken();
	TType tokenClass() const;
	bool start(char c, std::istream& istr);
	bool finish(std::istream& istr);
};


//
// inlines
//
inline const std::string& Token::tokenString() const
{
	return _value;
}


inline bool Token::is(Token::TType cls) const
{
	return tokenClass() == cls;
}


} 


#endif
